package com.hongkuncheng.cms.controller.admin;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.Form;
import com.hongkuncheng.cms.model.FormField;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 表单字段管理
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/formfield")
public class AformfieldController extends BaseController {

    // 表单字段列表
    @RequestMapping
    public String index() {
        int formId = Integer.parseInt(request.getParameter("formid"));
        Form form = factorySession.get(Form.class, formId);
        request.setAttribute("form", form);
        List<FormField> formfields = factorySession.createCriteria(FormField.class)
                .add(Restrictions.eq("form.id", formId)).list();
        request.setAttribute("formfields", formfields);
        return view();
    }

    // 表单字段编辑
    @RequestMapping("edit")
    public String edit() {
        int formId = Integer.parseInt(request.getParameter("formid"));
        Form form = factorySession.get(Form.class, formId);
        request.setAttribute("form", form);
        int formfieldId = request.getParameter("formfieldid") == null ? 0 :
                Integer.parseInt(request.getParameter("formfieldid"));
        FormField formfield = formfieldId == 0 ? new FormField() : factorySession.get(FormField.class, formfieldId);
        request.setAttribute("formfield", formfield);
        return view();
    }

    // 表单字段编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_webmaintain_form");
        int formId = Integer.parseInt(request.getParameter("formid"));
        int formfieldId = Integer.parseInt(request.getParameter("formfieldid"));
        FormField formfield = formfieldId == 0 ? new FormField() :
                factorySession.load(FormField.class, formfieldId, LockMode.PESSIMISTIC_WRITE);
        String name = request.getParameter("name");
        int count = Integer.parseInt(factorySession.createCriteria(FormField.class)
                .add(Restrictions.eq("form.id", formId))
                .add(Restrictions.eq("name", name))
                .add(Restrictions.ne("id", formfieldId))
                .setProjection(Projections.countDistinct("id"))
                .uniqueResult().toString());
        if (count > 0) {
            PageHelper.alertAndGoBack("字段名已存在，请重新输入");
            return;
        }
        if (formfieldId == 0) {
            Form form = factorySession.get(Form.class, formId);
            formfield.setForm(form);
            int index = 1;
            List<FormField> formfields = factorySession.createCriteria(FormField.class)
                    .add(Restrictions.eq("form.id", formId))
                    .addOrder(Order.asc("indexed"))
                    .list();
            for (FormField ftemp : formfields) {
                if (ftemp.getIndexed() == index) index++;
                else break;
            }
            formfield.setIndexed(index);
        }
        formfield.setName(name);
        formfield.setTitle(request.getParameter("title"));
        formfield.setNotnull(Boolean.parseBoolean(request.getParameter("notnull")));
        formfield.setListshow(Boolean.parseBoolean(request.getParameter("listshow")));
        if (formfieldId == 0) factorySession.save(formfield);
        recordLog((formId == 0 ? "新建" : "更新") + "表单字段《" + formfield.getName() + "》");
        PageHelper.alertAndRedirect("保存成功", "./?formid=" + formId);
    }

    // 表单字段删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_webmaintain_form");
        int formId = Integer.parseInt(request.getParameter("formid"));
        int formfieldId = Integer.parseInt(request.getParameter("formfieldid"));
        FormField formfield = factorySession.load(FormField.class, formfieldId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(formfield);
        recordLog("删除表单字段《" + formfield.getName() + "》");
        PageHelper.alertAndRedirect("删除成功", "./?formid=" + formId);
    }
}
